package lg.minecraft.plugin.pokedex.config;

import com.cobblemon.mod.common.pokemon.Species;
import lg.minecraft.plugin.cobblemoncore.util.PokemonDataCache;
import lg.minecraft.plugin.pokedex.Generations;
import lg.minecraft.plugin.pokedex.LGPokedex;
import lombok.Getter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author langle__
 * @version 1.0
 */
public class GenConfig {

    public static final GenConfig INSTANCE = new GenConfig();

    private final File folder;
    private final File gen1File;
    private final File gen2File;
    private final File gen3File;
    private final File gen4File;
    private final File gen5File;
    private final File gen6File;
    private final File gen7File;
    private final File gen8File;
    private final File gen9File;

    @Getter private List<Species> gen1;
    @Getter private List<Species> gen2;
    @Getter private List<Species> gen3;
    @Getter private List<Species> gen4;
    @Getter private List<Species> gen5;
    @Getter private List<Species> gen6;
    @Getter private List<Species> gen7;
    @Getter private List<Species> gen8;
    @Getter private List<Species> gen9;
    @Getter private List<Species> allGen;

    public GenConfig() {
        this.folder = new File(LGPokedex.getInstance().getDataFolder(), "generation");
        this.gen1File = new File(folder, "gen1.txt");
        this.gen2File = new File(folder, "gen2.txt");
        this.gen3File = new File(folder, "gen3.txt");
        this.gen4File = new File(folder, "gen4.txt");
        this.gen5File = new File(folder, "gen5.txt");
        this.gen6File = new File(folder, "gen6.txt");
        this.gen7File = new File(folder, "gen7.txt");
        this.gen8File = new File(folder, "gen8.txt");
        this.gen9File = new File(folder, "gen9.txt");
        saveDefault();
        loadConfig();
    }

    public void loadConfig() {
        gen1 = loadSpeciesFromFile(gen1File);
        gen2 = loadSpeciesFromFile(gen2File);
        gen3 = loadSpeciesFromFile(gen3File);
        gen4 = loadSpeciesFromFile(gen4File);
        gen5 = loadSpeciesFromFile(gen5File);
        gen6 = loadSpeciesFromFile(gen6File);
        gen7 = loadSpeciesFromFile(gen7File);
        gen8 = loadSpeciesFromFile(gen8File);
        gen9 = loadSpeciesFromFile(gen9File);
        allGen = Stream.of(gen1, gen2, gen3, gen4, gen5, gen6, gen7, gen8, gen9).flatMap(List::stream).toList();
    }

    public void saveDefault() {
        if (!folder.exists()) {
            if (!folder.mkdirs()) {
                LGPokedex.getInstance().severe("Failed to create generation folder.");
                return;
            }
        }
        saveDefault(gen1File);
        saveDefault(gen2File);
        saveDefault(gen3File);
        saveDefault(gen4File);
        saveDefault(gen5File);
        saveDefault(gen6File);
        saveDefault(gen7File);
        saveDefault(gen8File);
        saveDefault(gen9File);
    }

    public void saveDefault(File file) {
        if (file.exists()) return;
        LGPokedex.getInstance().saveResource("generation/" + file.getName(), false);
    }

    public List<Species> getSpeciesByGen(Generations generation) {
        return switch (generation) {
            case GEN1 -> gen1;
            case GEN2 -> gen2;
            case GEN3 -> gen3;
            case GEN4 -> gen4;
            case GEN5 -> gen5;
            case GEN6 -> gen6;
            case GEN7 -> gen7;
            case GEN8 -> gen8;
            case GEN9 -> gen9;
            case ALL_GEN -> allGen;
        };
    }

    private static List<Species> loadSpeciesFromFile(File file) {
        List<Species> speciesList = new ArrayList<>();
        PokemonDataCache pokemonDataCache = PokemonDataCache.getInstance();

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty() || line.startsWith("#")) {
                    continue;
                }
                Species species = pokemonDataCache.getSpeciesByKey(line);
                if (species != null) {
                    speciesList.add(species);
                } else {
                    LGPokedex.getInstance().getLogger().warning("Failed to get species '" + line + "' in file: " + file.getName());
                }
            }
        } catch (Exception e) {
            LGPokedex.getInstance().severe("Failed to read file: " + file.getName());
        }

        return speciesList;
    }

}
